package com.ddone.service.impl;

import com.ddone.bean.Order;
import com.ddone.feign.StockClient;
import com.ddone.mapper.OrderMapper;
import com.ddone.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.UUID;

/**
 * @author ddone
 * @date 2024/4/6-15:27
 */
@Slf4j
@Service
public class OrderServiceImpl implements OrderService {

    @Resource
    private OrderMapper orderMapper;

    @Resource
    private StockClient stockClient;

    @Override
    public Boolean saveOrder(String userId, String productId, Integer count) {
        Order order = Order.builder()
                .userId(userId)
                .productId(productId)
                .orderSn(UUID.randomUUID().toString().replace("-", ""))
                .count(count)
                .money(1000)
                .createTime(new Date())
                .build();
        orderMapper.save(order);
        // 更新库存
        String url = "http://spring-cloud-stock/stock/"+productId+"/"+count;
        Boolean result = stockClient.updateStock(productId, count);
        System.out.println("库存更新结果: "+result);
        return result != null;
    }
}
